<?php
namespace app\admin\controller;

use think\Controller;

class Role extends Controller
{

    private $_model;

    public function _initialize(){
        $this->_model = new \app\admin\model\Role();
    }

    /**
     * 角色列表
     */
    public function index()
    {
        $getLists = $this->_model->getLists();
        $this->assign('get_lists', $getLists);
        return $this->fetch('index');
    }

    /**
     * 添加新角色
     * @return mixed
     */
    public function add()
    {
        if(request()->isPost()){
            if($this->_model->add()){
                $this->success('添加成功', url('Role/index'), '', 1);
            }else{
                $this->error($this->_model->add()['msg']);
            }
        }
        return $this->fetch('add');
    }

    /**
     * 编辑角色
     * @return mixed
     */
    public function edit()
    {
        if(request()->post())
        {
            if($this->_model->edit()){
                $this->success('编辑成功', url('Role/index'), '', 1);
            }else{
                $this->error('编辑失败', url('Role/edit', 'id='.input('id')));
            }
        }else{
            //获取展示数据
            $editLists = $this->_model->getListById();
            $this->assign('edit_lists', $editLists);
            return $this->fetch('edit');
        }
    }

    /**
     * 删除角色
     * @return mixed
     */
    public function delete()
    {
        if($this->_model->delete()){
            $this->success('删除成功', url('Role/index'));
        }else{
            $this->error('删除失败', url('Role/index'));
        }

        return $this->fetch('index');
    }

    /**
     * 设置权限路由
     */
    public function set_power()
    {
        if(request()->isPost())
        {
            if($this->_model->setPower()){
                $this->success('设置成功', url('Role/index'));
            }else{
                $this->error('设置失败', url('Role/index'));
            }
        }else{
            //获取展示数据
            $this->assign('all_powers',$this->_model->getAllPower());
            $this->assign('own_powers',$this->_model->getAdminPower());
            $this->assign('id',input('id'));
            return $this->fetch('set_power');
        }
    }
}
